package com.example.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dao.RolePermission;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 功能描述
 * 角色权限mapper
 * @author: jimmy
 * @date: 2024年01月01日 17:05
 */
@Repository
public interface RolePermissionMapper extends BaseMapper<RolePermission> {
    /**
     * 根据角色id和权限id删除
     * @param rolePermission
     * @return
     */
    @Delete("<script>"+
            "delete " +
            " from " +
            "sys_roles_permissions " +
            "where role_id=#{roleId} and " +
            "permission_id=#{permissionId} " +
            "</script>")
    int deleteRolePermissionByBothId(RolePermission rolePermission);


    /**
     * 根据父目录删除其子目录下的权限分配
     * @param permissionId
     * @return
     */
    @Delete("<script>"+
            "delete from sys_roles_permissions  rp " +
            "where " +
            "exists ( " +
            "       select " +
            "1 " +
            "from " +
            "   sys_permissions p " +
            "where " +
            "p.id=rp.permission_id and " +
            "p.pid=#{permissionId}  )" +
            "</script>")
    int deleteRolePermissionByFirstId(Long permissionId);

    /**
     * 根据目录id查询角色，检查目录是否被角色设置了
     * @param permissionId
     * @return
     */
    @Select("<script>"+
            "select count(1) total " +
            " from " +
            "sys_roles_permissions " +
            "where  " +
            "permission_id=#{permissionId} " +
            "</script>")
    int findRoleCountByPermissionId(Long permissionId);

    /**
     * 查询所有角色及其对应的匹配字符串
     * @return
     */
    @Select("<script>"+
            "select " +
            "   r.role_name as roleName ,p.patchString as patchString " +
            "from " +
            "    sys_permissions p,sys_roles r,sys_roles_permissions rp " +
            "where " +
            "r.id=rp.role_id and rp.permission_id=p.id and p.pid is not null " +
            "</script>")
    List<RolePermission> findAllRolePermission();
}
